package com.algrithom.stack;

import java.util.Stack;

/**
 * 校验括号是否有效
 *
 * @author think
 */
public class Sollution1 {
    
    public static void main(String[] args){
        System.out.println(isValid("]()[({})]}"));
        System.out.println(isValid("([]"));
        System.out.println(isValid("[])"));
    }
    
    public static boolean isValid(String s){
        Stack<Character> stack = new Stack<>();
        boolean status = Boolean.FALSE;
        char[] chars = s.toCharArray();
        boolean hasOther = Boolean.FALSE;
        if (s.length() == 0) {
            return Boolean.TRUE;
        }
        for (char ch : chars) {
            if (ch == '(' || ch == '{' || ch == '[') {
                stack.push(ch);
            } else if (ch == ')' && stack.size() > 0 && stack.pop() == '(') {
                status = Boolean.TRUE;
            } else if (ch == '}' && stack.size() > 0 && stack.pop() == '{') {
                status = Boolean.TRUE;
            } else if (ch == ']' && stack.size() > 0 && stack.pop() == '[') {
                status = Boolean.TRUE;
            } else {
                hasOther = Boolean.TRUE;
                break;
            }
        }
        if (stack.size() > 0 || hasOther) {
            status = Boolean.FALSE;
        }
        return status;
    }
}
